package com.github.shiqiyue.app.api.sapi.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.shiqiyue.app.api.sapi.common.SApiBaseController;
import com.github.shiqiyue.app.api.sapi.req.role.RoleAddReq;
import com.github.shiqiyue.app.api.sapi.req.role.RoleEditReq;
import com.github.shiqiyue.app.api.sapi.result.role.RoleTreeDTO;
import com.github.shiqiyue.app.common.entity.TreeNode;
import com.github.shiqiyue.app.common.enums.EnableEnum;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.common.utils.TreeNodeUtils;
import com.github.shiqiyue.app.modules.sys.entity.Role;
import com.github.shiqiyue.app.modules.sys.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/***
 * 角色控制器
 * @author wenyao.wu
 * @date 2018/10/16
 */
@Slf4j
@Api(tags = "角色")
@RestController
@RequestMapping("sapi/sys/role")
public class SApiRoleController extends SApiBaseController {


    @Autowired
    private IRoleService roleService;

    /****
     * 角色列表
     * @return
     */
    @PreAuthorize("hasAuthority('0030100')")
    @ApiOperation("角色列表")
    @PostMapping("list")
    public Result<List<TreeNode>> list() {
        // 获取所有角色信息
        List<Role> roleList = roleService.list(new QueryWrapper<Role>().eq(Role.ENABLE, EnableEnum.ABLE));
        // 转换为vo
        List<RoleTreeDTO> roleTreeVOList = roleList
                .stream()
                .map(item -> RoleTreeDTO.newInstance(item))
                .collect(Collectors.toList());
        // 添加children属性
        List<TreeNode> treeNodeList = TreeNodeUtils.listToTree(roleTreeVOList);
        return Result.success(treeNodeList);
    }

    /***
     * 添加角色
     * @param roleAddReq
     * @return
     */
    @PreAuthorize("hasAuthority('0030101')")
    @ApiOperation("添加角色")
    @PostMapping("add")
    public Result addRole(@Valid RoleAddReq roleAddReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        if (!roleService.isCodeValid(roleAddReq.getCode())) {
            return Result.fail("编码已存在");
        }
        Role role = new Role();
        role.setCode(roleAddReq.getCode());
        role.setName(roleAddReq.getName());
        role.setPid(roleAddReq.getPid());
        role.setEnable(EnableEnum.ABLE);
        roleService.save(role);
        return Result.success();
    }

    /****
     * 修改角色
     * @param roleEditReq
     * @return
     */
    @PreAuthorize("hasAuthority('0030102')")
    @ApiOperation("修改角色")
    @PostMapping("edit")
    public Result editRole(@Valid RoleEditReq roleEditReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        if (!roleService.isCodeValid(roleEditReq.getCode(), roleEditReq.getId())) {
            return Result.fail("编码已存在");
        }
        Role updateParam = new Role();
        updateParam.setId(roleEditReq.getId());
        updateParam.setCode(roleEditReq.getCode());
        updateParam.setName(roleEditReq.getName());
        roleService.updateById(updateParam);
        return Result.success();
    }

    /***
     * 删除角色
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('0030103')")
    @ApiOperation("删除角色")
    @PostMapping("delete")
    public Result deleteRole(Long id) {
        roleService.delete(id, true);
        return Result.success();
    }

}
